/*
 * Copyright (c) 2025 李伟国
 * SQL-API is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY
 * KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
 * NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package com.moyu.sql.business.controller;

import com.moyu.sql.business.entity.MoyuSqlParam;
import com.moyu.sql.business.entity.MoyuSqlParamGroup;
import com.moyu.sql.business.entity.vo.MoyuSqlParamGroupDto;
import com.moyu.sql.business.service.IMoyuSqlParamService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/moyu/sql/param")
public class MoyuSqlParamController {

    @Resource
    private IMoyuSqlParamService moyuSqlParamService;

    /**
     * 获取分组下的sql列表
     *
     * @return sql列表
     */
    @GetMapping("list/{sqlParamGroupId}")
    public List<MoyuSqlParam> list(@PathVariable String sqlParamGroupId) {
        return moyuSqlParamService.lambdaQuery().eq(MoyuSqlParam::getParamGroupId, sqlParamGroupId).list();
    }

    /**
     * 获取SQL详情
     *
     * @return 详情
     */
    @GetMapping("/{id}")
    public MoyuSqlParam detail(@PathVariable String id) {
        return moyuSqlParamService.getById(id);
    }

    /**
     * 新增或更新
     *
     * @param moyuSqlParam 数据
     * @return 数据
     */
    @PostMapping("")
    public MoyuSqlParam save(@RequestBody MoyuSqlParam moyuSqlParam) {
        moyuSqlParamService.saveOrUpdate(moyuSqlParam);
        return moyuSqlParam;
    }

    /**
     * 批量新增或更新
     *
     * @param groupDto 参数分组列表
     * @return 参数分组
     */
    @PostMapping("batch")
    public MoyuSqlParamGroup saveBatch(@RequestBody MoyuSqlParamGroupDto groupDto) {
        return moyuSqlParamService.saveOrUpdateBatch(groupDto);
    }

    @GetMapping("group/{groupId}")
    public MoyuSqlParamGroupDto getByGroupId(@PathVariable String groupId) {
        return moyuSqlParamService.getByGroupId(groupId);
    }

    /**
     * 删除
     *
     * @param id 主键
     * @return 是否成功
     */
    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable String id) {
        return moyuSqlParamService.removeById(id);
    }
}
